#coding=utf-8
import crc16
import redis
from rediscluster import StrictRedisCluster
import random
import time
import sys

# redis-py will attempt to use the HiredisParser if you have the 
# hiredis module installed and will fallback to the PythonParser otherwise

host = '10.153.134.227'
port = 6379

cerberus_host = '10.77.44.158'
cerberus_port = 8810

r = redis.Redis(host,port,db=0) # 也可用ConnectionPool参数

def test_localhost():
	r = redis.Redis(cerberus_host,8810,db=0)

	# pipelines can also ensure the buffered commands are executed atomically 
	# as a group. This happens by default. If you want to disable the atomic
	# nature of a pipeline but still want to buffer commands, you can turn off transactions.
	pipe = r.pipeline()
	# pipe = r.pipeline(transactions=False)
	pipe.set('foo','bar3')
	pipe.set('foo2','bar4')
	# execute call send all buffered commands to server
	# return a list of responses, one for each command 
	pipe.execute()

def pressure_test():
	i = 0
	while i < 100:
		r.set('cs-k' + str(i),'cs-v' + str(i))
		i += 1

################################  cluster test 

x_cluster_port = 19308
x_cluster = [{"host":"10.221.48.251","port":x_cluster_port}]

#x_cluster_port = 17104
#x_cluster = [{"host":"10.121.76.67","port":x_cluster_port}]


def random_string(L):
	seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
	sa = []
	for i in range(L):
		sa.append(random.choice(seed))
		salt = ''.join(sa)
	return salt

def cluster_test():
	rc = StrictRedisCluster(startup_nodes=x_cluster,decode_responses=True)
	i = 0
	while i <= 100000:
		rc.set('cs-k' + str(i),random_string(16))
		i += 1
		#sleep(1)

def clear_all_cluster():
	rc = StrictRedisCluster(startup_nodes=x_cluster,decode_responses=True)
	
	
def proxy_test():
	r = redis.Redis(cerberus_host,8810,db=0)
	print sys.argv
	cnt = 10
	while cnt > 0:
		value = 'cs-v12-' + str(cnt)
		#old_value = 'cs-v2-' + str(cnt+1)
		#print 'slot:', crc16.crc16xmodem(key) % 16384
		#print r.get('cs-k' + str(cnt))
		'''
		if r.get('cs-k1') == old_value:
			print old_value + ' ok'
		else:
			print old_value + ' failed'
		'''
		try:
			if str(r.set('cs-k1',value)) == 'True':
				print 'set ' + value + ' ok'
			else:
				print 'set ' + value + ' not ok'
		except (Exception) as e:
			print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\t' + value + '\t' + e.message
		cnt = cnt -1

def proxy_pipeline_test():
	r = redis.Redis(cerberus_host,8810,db=0)
	pp = r.pipeline()
	cnt = 2000
	while cnt > 0:
		times = 10
		value = 'cs-v1-' + str(cnt)
		old_value = 'cs-v1-' + str(cnt+1)
		#print 'slot:', crc16.crc16xmodem(key) % 16384
		#print r.get('cs-k' + str(cnt))
		'''
		if r.get('cs-k1') == old_value:
			print old_value + ' ok'
		else:
			print old_value + ' failed'
		'''
		try:
			if str(r.set('cs-k1',value)) == 'True':
				print 'set ' + value + ' ok'
			else:
				print 'set ' + value + ' not ok'
		except:
			print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\t' + value + ' except'
		cnt = cnt -1


if __name__ == '__main__':
	clear_all_cluster();
	#proxy_test()	
	#test_localhost()
	#cluster_test()
	#pressure_test()
